package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.MonitorModuleApplication;
import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.QualityInspector;
import com.af.monitorModule.generator.entity.Company;
import com.af.monitorModule.generator.entity.TimedTask;
import com.af.monitorModule.generator.entity.TimedTaskResult;
import com.af.monitorModule.generator.mapper.CompanyMapper;
import com.af.monitorModule.generator.mapper.TimedTaskMapper;
import com.af.monitorModule.generator.mapper.TimedTaskResultMapper;
import com.af.monitorModule.generator.service.TimedTaskResultService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-03-05
 */
@Service
public class TimedTaskResultServiceImpl extends ServiceImpl<TimedTaskResultMapper, TimedTaskResult> implements TimedTaskResultService {

    private static final Logger logger = LoggerFactory.getLogger(TimedTaskResultServiceImpl.class);

    @Autowired
    private TimedTaskMapper timedTaskMapper;

    @Autowired
    private CompanyMapper companyMapper;


    @Autowired
    private QualityInspector qualityInspector;

    @Override
    public JSONObject setResult(TimedTaskResult timedTaskResult) {
        if (timedTaskResult == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        if (this.save(timedTaskResult)){
            try {
                TimedTask timedTask = timedTaskMapper.selectById(timedTaskResult.getTaskId());
                Company company = companyMapper.selectById(timedTask.getCompanyId());
                StringBuilder text = new StringBuilder()
                        .append("任务名称：").append(timedTask.getTaskName()).append("\n")
                        .append("任务说明：").append(timedTask.getTaskExplain()).append("\n")
                        .append("任务类型：").append(timedTask.getTaskType()).append("\n")
                        .append("此次执行时间：").append(DateTimeFormatter.ofPattern("HH:mm:ss").format(timedTaskResult.getRunTime().toLocalTime())).append("\n")
                        .append("执行结果：").append(timedTaskResult.getRunResult() == 0 ? "失败": "成功").append("\n")
                        .append("结果说明：").append(timedTaskResult.getRunInformation());
                qualityInspector.inspect(new JSONObject() {{
                    put("isText",true);
                    put("isSpeak",true);
                    put("text", text.toString());
                    put("speak","定时任务执行结果已返回,请查看");
                    put("time", DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalTime.now()));
                    put("company", company.getCompanyName());
                }});
            }catch (Exception e){
                logger.error("推送大屏失败", e);
            }
            return ResultEnum.OK.getResult();
        }else {
            return ResultEnum.CREATE_ERROR.getResult();
        }

    }

    @Override
    public JSONObject getResult(String id) {
        if (id == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        QueryWrapper<TimedTaskResult> wrapper = new QueryWrapper<>();
        wrapper.eq("task_id",Integer.valueOf(id));
        wrapper.orderByDesc("run_time");
        List<TimedTaskResult> list = this.list(wrapper);
        JSONObject res= new JSONObject();
        res.put("data",list);
        res.put("code",0);
        res.put("count",list.size());
        res.put("msg","执行列表");
        return res;
    }
}
